<?php

$base = base_url();
$kelas = $this->uri->segment(1);
$fungsi = $this->uri->segment(2);

$option_jenis_device = models\Jenis_device::get_dropdown_options();
$option_objek = models\Objek_pengecekan::get_dropdown_options();

?>

<script type='text/javascript'>

var objek_counter = 1;
var device_counter = 1;

function create_new_objek(label,index){
	new_element = 
		'<div class ="item field">' +
		'<label>' + label + '</label>' +
		'<input type="hidden" value="'+ label +'" name="newnama_'+ index +'">' +
		'<select name="newkond_'+ index +'">' +
			'<option selected="selected" value="ok">OK</option>' +
			'<option value="nok">Not OK</option>' +
		'</select>' +
		'<textarea rows="12" cols="90" name="newrema_'+ index +'"></textarea>' +
		'<button type="button" class="delete_baru" name="delete_baru">x</button>' +
		'</div>';
	return new_element;
}

function create_new_objek_adder(){
	new_element = 
		'<div class="inside_block field" style="">' +
			'<div class="item">' +
				'<label for="nama_objek_baru">+ objek</label>' +
				'<select name="nama_objek">' +
				<?php foreach($option_objek as $value => $label):?>
					'<option value="<?php echo $value ?>"><?php echo $label ?></option>' +
				<?php endforeach ?>
					'<option value="custom">Custom</option>' +
				'</select>' +
				'<input type="text" style="display:none" disabled="" value="" name="nama_objek_baru" />' +
				'<button type="button" name="ok_baru">OK</button>' +
			'</div>' +
		'</div>' +
		'<div class="clear"></div>' +
		'<input class="field" type="submit" value="Tambah" name="tambah">';
	return new_element;
}

function create_new_device_objek(id){
	new_element = 
		'<div class="block" id="block_'+ id +'">' +
			'<form id="'+ id +'" method="post" action="<?php echo "$base$kelas/ajax_tambah_checklist/$kunjungan_id" ?>">' +
				'<div class="inside_block device_info">' +
					'<div class="item field"> ' +
						'<label>Nama Device</label> ' +
						'<input type="text" name="nama_device" />' +
					'</div> ' +
					'<div class="item field"> ' +
						'<label>Jenis Device</label> ' +
						'<select name="jenis_device"> ' +
						<?php foreach($option_jenis_device as $value => $label):?>
							'<option value="<?php echo $value ?>"><?php echo $label ?></option>' +
						<?php endforeach ?>
						'</select>' +
						'<button type="button" name="ok">generate default</button>' +
					'</div> ' +
				'</div>' +
			'</form>' +
		'</div>';
	return new_element;
}

function add_new_objek(element,index){

	var block = $(element).parent().parent();
	var toggle = $(element).parent().find('select[name="nama_objek"]').find('option:selected').attr('value');
	var manual_input = $(element).parent().find('input[name="nama_objek_baru"]').attr('value');

	if(toggle == 'custom')
		var label = manual_input;
	else var label = toggle;

	new_element = create_new_objek(label,index);
	block.before(new_element);

	$('.delete_baru').click(function(e){
		$(this).parent().remove();
	});

}

function submit_action(element){

	var form = $(element).parent();
	var form_id = form.attr('id');
	var tipe = form_id.substr(0,12);

	if(form_id == 'add_teknisi'){
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: form.attr('action'),
			data: form.serializeArray(), 
			success: function(data){
				window.location.href = '<?php echo current_url() ?>';
			}
		});
	} else if(form_id == 'edit_teknisi' ){
		$.ajax({
			type: 'POST',
			dataType: 'html',
			url: form.attr('action'),
			data: form.serializeArray(), 
			success: function(data){
				form.parent().html(data);
				new_form = $('#edit_teknisi');
				init_form_script(new_form);
			}
		});
	} else if(tipe == 'edit_chklist') {
		var id = form_id.substr(13);
		$.ajax({
			type: 'POST',
			dataType: 'html',
			url: form.attr('action') + '/' + id,
			data: form.serializeArray(), 
			success: function(data){
				form.parent().html(data);
				new_form = $('#edit_chklist_' + id);
				init_form_script(new_form);
			}
		});
	} 
}

function edit_click(element){
	$(element).parent().parent().find('.info, .field').toggle();
	if($(element).text() == 'edit') 
		$(element).text('cancel');
	else if($(element).text() == 'cancel') 
		$(element).text('edit');
}

function delete_click(element){
	var r = confirm("Apakah anda yakin ingin menghapus checklist ini?\n(Warning! Tidak bisa dibatalkan)");
	if(r == true){
		$.ajax({
			type: 'POST',
				dataType: 'json',
				url: $(element).attr('href'),
			success: function(data){
				$(element).parent().parent().parent().remove();
			}
		});
	}
}

function custom_toggle(element){
	var toggle = $(element).find('option:selected').attr('value');
	var manual_input = $(element).parent().find('input[name="nama_objek_baru"]');
	if(toggle == 'custom'){
		manual_input.removeAttr('disabled');
		manual_input.show();
	} else {
		manual_input.attr('disabled',true);
		manual_input.hide();
	}
}

function init_form_script(form){
	form.find('.edit_menu').click(function(e){
		e.preventDefault();
		edit_click(this);
	});
	form.find('.delete_menu').click(function(e){
		e.preventDefault();
		delete_click(this);
	});
	form.find('input[type="submit"]').click(function(e){
		e.preventDefault();
		submit_action(this);
	});
	form.find('button[name="ok_baru"]').click(function(e){
		add_new_objek(this,objek_counter); 
		objek_counter++;
	});
	form.find('select[name="nama_objek"]').change(function(e){
		custom_toggle(this);
	});
}

$('.block .edit_menu').click(function(e){
	e.preventDefault();
	edit_click(this);
});

$('.block .delete_menu').click(function(e){
	e.preventDefault();
	delete_click(this);
});

$('form input[type="submit"]').click(function(e){
	e.preventDefault();
	submit_action(this);
});

$('select[name="nama_objek"]').change(function(e){
	custom_toggle(this);
});

$('button[name="ok_baru"]').click(function(e){
	add_new_objek(this,objek_counter); 
	objek_counter++;
});

$('#delete_kunjungan_menu').click(function(e){
	e.preventDefault();
	var r = confirm("Apakah anda yakin ingin menghapus kunjungan ini?\n(Warning! Tidak bisa dibatalkan)");
	if(r == true){
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: $(this).attr('href'),
			success: function(data){
				window.location.href = '<?php echo current_url() ?>';
			}
		});
	}
});

$('#tambah_checklist_menu').click(function(e){
	e.preventDefault();
	var main_content = $('#mainContent');
	var id = 'new_cl_' + device_counter;
	device_counter++;

	var new_element = create_new_device_objek(id);
	main_content.append(new_element);

	$('#'+ id +' input[name="nama_device"]').focus();
	$('#'+ id +' button[name="ok"]').click(function(e){
		var form = $('#' + id);
		var block = $(this).parent().parent();
		var jenis = $('#'+ id + ' select[name="jenis_device"]').find('option:selected').attr('value');
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: '<?php echo "$base$kelas/ajax_get_default_objek/" ?>' + jenis,
			success: function(data){

				for(index in data){
					new_element = create_new_objek(data[index],index);
					form.append(new_element);
				}
				var counter = parseInt(index) + 1;

				$('.delete_baru').click(function(e){
					$(this).parent().remove();
				});

				new_element = create_new_objek_adder();
				form.append(new_element);

				init_form_script(form);

				form.find('input[type="submit"]').click(function(e){
					e.preventDefault();
					$.ajax({
						type: 'POST',
						dataType: 'html',
						url: form.attr('action'),
						data: form.serializeArray(), 
						success: function(data){
							form.parent().html(data);
							new_form = $('#block_' + id).find('form');
							init_form_script(new_form);
						}
					});
				});

			}
		});
		$(this).remove();
	});
});

<?php if(models\Current_user::previlege('Teknisi')){ ?>
	$('#tambah_kunjungan_menu').click(function(e){
		e.preventDefault();
		$.ajax({
			type: 'POST',
				url: $(this).attr('href'),
				success: function(data){
					window.location.href = '<?php echo current_url() ?>';
				}
		});
	});
<?php } else if(models\Current_user::previlege('Administrator|Agen')){ ?>
	$('#tambah_kunjungan_menu').click(function(e){
		e.preventDefault();
		$('#tambah_kunjungan_block').toggle();
	});
<?php } ?>

</script>
